package xyj.sparrow.scheduler.cron.stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import xyj.sparrow.scheduler.cron.ops.OpsMsg;

import javax.annotation.Resource;

/**
 * 提供者
 * 用于注册Cron集合操作事件 消息
 *
 * @author 江南小俊
 * @since 2021/6/20 下午11:50
 **/
@Slf4j
public class CronProvider {
    @Resource
    private StringRedisTemplate redisTemplate;

    /**
     * 发送消息
     *
     * @param streamKey 消息key
     * @param ops       消息内容
     * @author 江南小俊
     * @since 2021/6/20 下午11:55
     **/

    public void sendOpsMsg(String streamKey, OpsMsg ops) {
        ObjectRecord<String, OpsMsg> record = ObjectRecord.create(streamKey, ops);
        RecordId recordId = redisTemplate.opsForStream().add(record);
        log.info("发送消息：{}", recordId);
    }
}
